package com.ygsoft.tianlu_mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.pojo.vo.req.DeleteCollectGoodsReqVO;
import com.ygsoft.tianlu_mall.pojo.vo.req.DeleteCollectShopReqVO;
import com.ygsoft.tianlu_mall.service.CollectService;
import com.ygsoft.tianlu_mall.service.NumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CollectController {

    @Autowired
    CollectService collectService;

    @Autowired
    NumService numService;

    //商品收藏部分

    //收藏商品
    @RequestMapping(value = "/collectGoods",method = RequestMethod.POST)
    @ResponseBody
    public Msg collectGoods(@RequestParam int user_id,@RequestParam int goods_id){
        //检查商品是否存在数据库
        if (collectService.checkCollectedGoodsOnDB(goods_id)){
            //检查商品是否已经被收藏了
            if (collectService.checkCollectedGoods(user_id,goods_id)){
                collectService.collectgoods(user_id ,goods_id);
                return Msg.succes();
            }else {
                String message = "请勿重复收藏商品！";
                return Msg.fail().add("message",message);
            }
        }else {
            String message = "请勿收藏不存在的商品！";
            return Msg.fail().add("message",message);
        }

    }

    //删除收藏夹一个商品
    @RequestMapping(value = "/deleteOneGoods",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteOneGoods(@RequestParam int user_id,@RequestParam int goods_id){
        //检查商品是否在收藏夹内，在则允许删除
        if (!collectService.checkCollectedGoods(user_id,goods_id)){
            collectService.deleteOneGoods(user_id,goods_id);
            return Msg.succes();
        }else {
            String message = "删除失败,请不要删除未被收藏的商品！";
            return Msg.fail().add("message",message);
        }
    }

    //删除收藏夹多个商品
    //return 结果有bug 返回list中存在无效goods_id不会返回正确的提示信息
    @RequestMapping(value = "/deleteMoreGoods",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteMoreGoods(@RequestBody DeleteCollectGoodsReqVO vo){
            /*json
            {
                "user_id" :1,
                "goods_idList" :[
                    1,2,3
                    ]
            }
            *
            * */
        try {
            for (int goods_id:vo.getGoods_idList()) {
                HashMap<String,Object> goods_idMap = new HashMap<>();
                goods_idMap.put("goods_id",goods_id);
                deleteOneGoods(vo.getUser_id(),goods_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //查询用户收藏夹内的商品
    @RequestMapping(value = "/selectCollectGoods",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectCollectGoods(@RequestParam int user_id,
                                  @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam Integer pageSize){
        try {
            List<HashMap<String,Object>> goodsNum = new ArrayList<>();
            HashMap<String,Object> result = new HashMap<>();
            PageHelper.startPage(pageNum,pageSize);
            List<Map<String,Object>>  collectGoodslist = collectService.selectCollectGoods(user_id);
            List goods_idList = collectService.getGoodsIdByUserId(user_id);
            for (Object goods_id:goods_idList){
                HashMap<String,Object> goodsNumMap =numService.getThreeNum((int)goods_id);
                goodsNum.add(goodsNumMap);
            }
            PageInfo collectGoodsList = new PageInfo(collectGoodslist,5);
            result.put("collectGoodsList",collectGoodsList);
            result.put("goodsNum",goodsNum);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //统计收藏商品数目
    @RequestMapping(value = "/getCollectGoodsCount",method = RequestMethod.GET)
    @ResponseBody
    public Msg getCollectGoodsCount(@RequestParam int user_id){
        try {
            int count = collectService.getCollectGoodsCount(user_id);
            HashMap<String,Object> result = new HashMap<>();
            result.put("count",count);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //店铺收藏部分

    //收藏店铺
    @RequestMapping(value = "/collectShop",method = RequestMethod.POST)
    @ResponseBody
    public Msg collectShop(@RequestParam int user_id,@RequestParam int shop_id){
        //检查店铺是否存在数据库
        if (collectService.checkCollectedShopOnDB(shop_id)){
            //检查商品是否已经被收藏了
            if (collectService.checkCollectedShop(user_id,shop_id)){
                collectService.collectshop(user_id ,shop_id);
                return Msg.succes();
            }else {
                String message = "请勿重复收藏店铺！";
                return Msg.fail().add("message",message);
            }
        }else {
            String message = "请勿收藏不存在的店铺！";
            return Msg.fail().add("message",message);
        }

    }

    //删除收藏夹一个商品
    @RequestMapping(value = "/deleteOneShop",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteOneShop(@RequestParam int user_id,@RequestParam int shop_id){
        //检查店铺是否在收藏夹内，在则允许删除
        if (!collectService.checkCollectedShop(user_id,shop_id)){
            collectService.deleteOneShop(user_id,shop_id);
            return Msg.succes();
        }else {
            String message = "删除失败,请不要删除未被收藏的商铺！";
            return Msg.fail().add("message",message);
        }
    }

    //删除收藏夹多个商品
    @RequestMapping(value = "/deleteMoreShop",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteMoreShop(@RequestBody DeleteCollectShopReqVO vo){
            /*json
            {
                "user_id" :1,
                "shop_idList" :[
                    1,2
                    ]
            }
            *
            * */
        try {
            for (int shop_id:vo.getShop_idList()) {
                HashMap<String,Object> shop_idMap = new HashMap<>();
                shop_idMap.put("shop_id",shop_id);
                deleteOneShop(vo.getUser_id(),shop_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //查询用户收藏夹内的店铺
    //缺用户头像
    @RequestMapping(value = "/selectCollectShop",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectCollectShop(@RequestParam int user_id,
                                 @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                 @RequestParam Integer pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Map<String,Object>>  collectShopMap = collectService.selectCollectShop(user_id);
            List<Map<String,Object>>  collectShopGoodsMap = collectService.collectShopGood(user_id);
            List shoplist = new ArrayList();
            shoplist.add(collectShopMap);
            shoplist.add(collectShopGoodsMap);
            PageInfo goodsList = new PageInfo(shoplist,5);
            HashMap<String,Object> result = new HashMap<>();
            result.put("shopList",goodsList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //统计收藏商品数目
    @RequestMapping(value = "/getCollectShopCount",method = RequestMethod.GET)
    @ResponseBody
    public Msg getCollectShopCount(@RequestParam int user_id){
        try {
            int count = collectService.getCollectShopCount(user_id);
            HashMap<String,Object> result = new HashMap<>();
            result.put("count",count);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
}
